package com.zmn.oms.dubbo.impl.normal.master;

import com.alibaba.fastjson.JSON;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.business.interfaces.work.masterwork.MasterWorkContactBService;
import com.zmn.oms.common.dio.normal.master.WorkContactInfoDIO;
import com.zmn.oms.common.dro.normal.master.NonGridGrabOrderDRO;
import com.zmn.oms.common.dro.normal.master.WorkContactInfoDRO;
import com.zmn.oms.common.dio.normal.query.GrabQueryDIO;
import com.zmn.oms.common.dro.normal.master.GrabOrderDRO;
import com.zmn.oms.common.dro.normal.master.ReworkRefundDRO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.dubbo.interfaces.normal.master.MasterOrderWorkListRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.dto.graborder.GrabOrderDTO;
import com.zmn.oms.model.dto.graborder.GrabWorkQuery;
import com.zmn.oms.zmn.business.interfaces.work.OrderWorkGrabBService;
import com.zmn.oms.zmn.normal.business.interfaces.refund.RefundBService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 类描述：
 *
 * @author wangxiaokun
 * @since 2021/07/15 16:26
 */
@Slf4j
@Service(loadbalance = "roundrobin", timeout = 10000, retries = 2, version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class MasterOrderWorkListRemoteServiceImpl implements MasterOrderWorkListRemoteService {

    @Autowired
    private OrderWorkGrabBService orderWorkGrabBService;
    @Autowired
    private MasterWorkContactBService masterWorkContactBService;
    @Autowired
    private RefundBService refundBService;

    /**
     * 工程师查询抢单列表
     *
     * @param grabQueryDIO
     * @return
     */
    @Override
    public ResponseDTO<List<GrabOrderDRO>> listGrabOrderWorkByQuery(@NotNull(message = "入参不能为空") GrabQueryDIO grabQueryDIO) {
        log.debug("【抢单】查询抢单列表入参：【{}】", JSON.toJSONString(grabQueryDIO));
        try {
            GrabWorkQuery grabWorkQuery = BeanMapper.map(grabQueryDIO, GrabWorkQuery.class);
            List<GrabOrderDTO> grabOrderList = orderWorkGrabBService.listGrabOrderByMasterInfo(grabWorkQuery);
            List<GrabOrderDRO> droList = BeanMapper.mapList(grabOrderList, GrabOrderDRO.class);
            log.debug("【抢单】查询抢单列表出参：【{}】", JSON.toJSONString(droList));
            return ResponseDTO.success(droList);
        } catch (OmsBaseException e) {
            return ResponseDTO.fail(e.getCode(), e.getMessage());
        } /*catch (UncategorizedMongoDbException e) {
            return ResponseDTO.fail(OmsErrorCodeConsts.ERROR_ENGINEER_INFO_CODE,"工程师服务区域不规范");
        }*/
    }

    @Override
    public ResponseDTO<List<NonGridGrabOrderDRO>> listNonGridGrabOrderWorkByQuery(GrabQueryDIO grabQueryDIO) {
        log.info("【抢单】查询无网格抢单列表入参：【{}】", grabQueryDIO);
        try {
            List<NonGridGrabOrderDRO> grabOrderList = orderWorkGrabBService.listNonGridGrabOrder(grabQueryDIO);
            log.debug("【抢单】查询抢单列表出参：【{}】", JSON.toJSONString(grabOrderList));
            return ResponseDTO.success(grabOrderList);
        } catch (OmsBaseException e) {
            return ResponseDTO.fail(e.getCode(), e.getMessage());
        }
    }

    /**
     * 返修单获取退款配置
     * @param orderId
     * @param workId
     * @return
     */
    @Override
    public ResponseDTO<ReworkRefundDRO> getReworkRefundConfig(Long orderId, Long workId) {
        try {
            return ResponseDTO.success(refundBService.getReworkRefundConfig(orderId, workId));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("[{}]获取返修单退款配置失败：【{}】", workId, e.getMessage());
            return ResponseDTO.fail();
        }
    }

    @Override
    public ResponseDTO<WorkContactInfoDRO> getMasterWorkContactInfo(@NotNull(message = "入参不能为空") WorkContactInfoDIO workContactInfoDIO) {
        try {
            WorkContactInfoDRO workContactInfo = masterWorkContactBService.getWorkContactInfo(workContactInfoDIO);
            return ResponseDTO.success(workContactInfo);
        } catch (OmsBaseException e) {
            return ResponseDTO.fail(e.getMessage());
        }
    }
}
